package com.csun.nusing.presenter;

import android.content.Context;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.csun.nusing.R;
import com.csun.nusing.bean.UrlBean;
import com.csun.nusing.bean.jsonbean.NursingOldrestListJsonBean;
import com.csun.nusing.bean.jsonbean.NursingOldrestListJsonBean.NursingOldrestDataBean.NursingOldrestListBean;
import com.csun.nusing.bean.jsonbean.OldrestStateJsonBean;
import com.csun.nusing.presenter.inter.IHome;
import com.csun.nusing.ui.adapter.MyBaseAdapter;
import com.csun.nusing.ui.myview.TabLayout;
import com.csun.nusing.ui.myview.TabLayout.OnSelectedCallBack;
import com.csun.nusing.util.SPUtils;
import com.google.gson.Gson;
import com.home.administrator.okhttplibrary.IOkTaskCallback;
import com.home.administrator.okhttplibrary.OkHttpTask;
import com.home.administrator.okhttplibrary.OkHttpTask.OkHttpTaskBuidler.OkType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 在护工主界面的第一个主页fragment的presenter层 处理里面的业务逻辑 ClassName:
 * FragmentNursingHomePresenter
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-10-20
 */
public class FragmentNursingHomePresenter extends BasePresenter<IHome> {

	private Context mcontext;
	private String authorization;
	private String userid;
	protected NursingOldrestListJsonBean nursingoldrestlistbean;
	protected OldrestStateJsonBean oldreststatebean;
	private TextView bloodoxyTv;
	private TextView bloodpressTv;
	private TextView bloodsugerTv;
	private TextView heartrateTv;
	private TextView huxiTv;
	private TextView temperatureTv;
	private ImageView photoimageView;
	private TextView nameTv;
	private TabLayout tablay;
	private MyBaseAdapter<NursingOldrestListBean> mAdapter;
	private int currentposition;
	private NursingOldrestListBean dataBean;
	private String roomCode;

	public FragmentNursingHomePresenter(IHome view) {
		super(view);
	}

	public void getOlderList(Context context, TabLayout horlv,
			TextView bloodoxyTv, TextView bloodpressTv, TextView bloodsugerTv,
			TextView heartrateTv, TextView huxiTv, TextView temperatureTv,
			ImageView photoimageView, TextView nameTv) {
		this.mcontext = context;
		this.tablay = horlv;
		this.bloodoxyTv = bloodoxyTv;
		this.bloodpressTv = bloodpressTv;
		this.bloodsugerTv = bloodsugerTv;
		this.heartrateTv = heartrateTv;
		this.huxiTv = huxiTv;
		this.temperatureTv = temperatureTv;
		this.photoimageView = photoimageView;
		this.nameTv = nameTv;

		/**
		 * 1获取看护人列表 2.获取健康数据 3.将健康数据显示
		 */

		authorization = (String) SPUtils.get(context, "authorization", "");
		userid = (String) SPUtils.get(context, "userId", "");
		Map<String, String> param = new HashMap<String, String>();
		param.put("userid", "" + userid);
		param.put("pageNo", "1");
		param.put("pageSize", "10000");

		OkHttpTask.newInstance(context).post(param, OkType.form)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Log.e("fzq", "-------" + result);
							Gson gson = new Gson();
							nursingoldrestlistbean = gson.fromJson(result,
									NursingOldrestListJsonBean.class);
							if (nursingoldrestlistbean != null) {
								if (nursingoldrestlistbean.getData() != null) {
									nursingoldrestlistbean.getData().getList();
									getHealthList(nursingoldrestlistbean);
									controlList(nursingoldrestlistbean);
								}
							}
						}
					}
				}).start(UrlBean.NURSINGBYOLDMANURL);
	}

	/**
	 * 获取健康报告
	 * 
	 * @Description: TODO
	 * @param @param nursingoldrestlistbean2
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-30
	 */
	protected void getHealthList(
			final NursingOldrestListJsonBean nursingoldrestlistbean2) {
		// 加载第一个
		if (nursingoldrestlistbean2.getData().getList().size() > 0) {
			initData(nursingoldrestlistbean2.getData().getList(), 0);
		}

	}

	/**
	 * 加载数据
	 * 
	 * @Description: TODO
	 * @param @param list
	 * @param @param i
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-30
	 */
	private void initData(List<NursingOldrestListBean> list, int i) {
		currentposition = i;
		Log.e("fzq", "-----home  initData");
		if (i < list.size()) {
			/**
			 * 传数据到view
			 */
			dataBean = list.get(i);
			roomCode = "-";
			if (dataBean.getBuildingName() == null) {
				roomCode = "-";
			} else if (dataBean.getRoomNo() == null) {
				roomCode = "-";
			} else if (dataBean.getBedNo() == null) {
				roomCode = "-";
			} else if (dataBean.getBuildingName().equals("null")) {
				roomCode = "-";
			} else if (dataBean.getRoomNo().equals("null")) {
				roomCode = "-";
			} else if (dataBean.getBedNo().equals("null")) {
				roomCode = "-";
			} else {
				roomCode = dataBean.getBuildingName()
						+ dataBean.getRoomNo() + mcontext.getString(R.string.room) + dataBean.getBedNo()
						+ mcontext.getString(R.string.bed);
			}
			/**
			 * 传数据到view
			 */
			getViewRef().sendId("" + dataBean.getId(), "" + dataBean.getName(),
					"" + dataBean.getHeadPicture(),
					"" + dataBean.getBirthday(), "" + dataBean.getAge(),
					"" + roomCode, "" + dataBean.getSex(),
					"" + dataBean.getEgContact(), "" + dataBean.getEgPhone(),
					"" + dataBean.getRoomId(),""+dataBean.getBedNo());
			nameTv.setText(""
					+ nursingoldrestlistbean.getData().getList().get(i)
							.getName());
			Glide.with(mcontext)
					.load(nursingoldrestlistbean.getData().getList().get(i)
							.getHeadPicture()).into(photoimageView);

			/**
			 * 请求
			 */
			Map<String, String> param = new HashMap<String, String>();
			param.put("id", "" + list.get(i).getId());
			param.put("headpic", "" + list.get(i).getHeadPic());
			Log.e("fzq", "-----id  " + list.get(i).getId() + "--- headpic"
					+ list.get(i).getHeadPic());
			if (list.get(i).getHeadPicture() == null) {
				return;
			}
			OkHttpTask.newInstance(mcontext).post(param, OkType.form)
					.addHeader("authorization", authorization)
					.enqueue(new IOkTaskCallback() {
						@Override
						public void onSuccess(String result) {
							if (result != null) {
								Log.e("fzq", "-----home  " + result);
								Gson gson = new Gson();
								oldreststatebean = gson.fromJson(result,
										OldrestStateJsonBean.class);
								if (oldreststatebean != null) {
									controlViewpager(oldreststatebean);
								}
							}
						}
					}).start(UrlBean.OLDRESTINFOURL);
		}
	}

	/**
	 * 显示首页的数据
	 * 
	 * @Description: 展示首页老人列表
	 * @param @param oldreststatebean2
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-11
	 */
	protected void controlViewpager(OldrestStateJsonBean oldreststatebean2) {
		if (oldreststatebean2.getData() == null) {
			return;
		}
		if (oldreststatebean2.getData().getHeadpicture() != null) {
			Glide.with(mcontext)
					.load(oldreststatebean2.getData().getHeadpicture())
					.into(photoimageView);
		}
		bloodoxyTv
				.setText(""
						+ (oldreststatebean2.getData().getBoxvygenStatus() != null ? oldreststatebean2
						.getData().getBoxvygenStatus() : mcontext.getString(R.string.bed_detail_none)));
		bloodpressTv
				.setText(""
						+ (oldreststatebean2.getData().getBpressureStatus() != null ? oldreststatebean2
						.getData().getBpressureStatus() : mcontext.getString(R.string.bed_detail_none)));
		bloodsugerTv
				.setText(""
						+ (oldreststatebean2.getData().getBsugarStatus() != null ? oldreststatebean2
						.getData().getBsugarStatus() : mcontext.getString(R.string.bed_detail_none)));
		heartrateTv
				.setText(""
						+ (oldreststatebean2.getData().getHeartrateStatus() != null ? oldreststatebean2
						.getData().getHeartrateStatus() : mcontext.getString(R.string.bed_detail_none)));
		huxiTv.setText(""
				+ (oldreststatebean2.getData().getBreatheStatus() != null ? oldreststatebean2
				.getData().getBreatheStatus() : mcontext.getString(R.string.bed_detail_none)));
		temperatureTv
				.setText(""
						+ (oldreststatebean2.getData().getTemperatureStatus() != null ? oldreststatebean2
						.getData().getTemperatureStatus() : mcontext.getString(R.string.bed_detail_none)));
		nameTv.setText(""
				+ (oldreststatebean2.getData().getOldName() != null ? oldreststatebean2
				.getData().getOldName() : mcontext.getString(R.string.bed_detail_none)));

	}

	/**
	 * 控制viewpager和tab
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-30
	 */
	private void controlList(
			final NursingOldrestListJsonBean nursingoldrestlistbean) {
		for (int i = 0; i < nursingoldrestlistbean.getData().getList().size(); i++) {
			tablay.addTab(i, nursingoldrestlistbean.getData().getList().get(i)
					.getName());

		}
		tablay.initStrip();
		tablay.setOnSelectedCallBack(new OnSelectedCallBack() {
			@Override
			public void selected(int position) {
				initData(nursingoldrestlistbean.getData().getList(), position);
				dataBean = nursingoldrestlistbean.getData().getList()
						.get(position);
				roomCode = "-";
				if (dataBean.getBuildingName() == null) {
					roomCode = "-";
				} else if (dataBean.getRoomNo() == null) {
					roomCode = "-";
				} else if (dataBean.getBedNo() == null) {
					roomCode = "-";
				} else if (dataBean.getBuildingName().equals("null")) {
					roomCode = "-";
				} else if (dataBean.getRoomNo().equals("null")) {
					roomCode = "-";
				} else if (dataBean.getBedNo().equals("null")) {
					roomCode = "-";
				} else {
					roomCode = dataBean.getBuildingName()
							+ dataBean.getRoomNo() + mcontext.getString(R.string.room) + dataBean.getBedNo()
							+ mcontext.getString(R.string.bed);
				}
				/**
				 * 传数据到view
				 */
				getViewRef().sendId("" + dataBean.getId(),
						"" + dataBean.getName(),
						"" + dataBean.getHeadPicture(),
						"" + dataBean.getBirthday(), "" + dataBean.getAge(),
						"" + roomCode, "" + dataBean.getSex(),
						"" + dataBean.getEgContact(),
						"" + dataBean.getEgPhone(), "" + dataBean.getRoomId(),""+dataBean.getBedNo());
			}
		});

	}
}
